var vm = new Vue({
    el: '#usercenter_info',
    data: {
        
        histories: [],
        host,
        username: '',
        mobile: '',
        email: '',
        last_login:'',
        email_active: false,
        set_email: false,
        send_email_btn_disabled: false,
        send_email_tip: '重新发送验证邮件',
        email_error: false,
        error_name:false,
        error_name_message:'',
        error_mobile:false,
        error_mobile_message:'',
        error_eamil:false,
        error_email_message:'',
        name:'',
        phone:'',
        e_mail:'',
    },
    mounted() {
        获取个人信息:
        this.get_person_info()
        // this.get_history()
    },
    methods: {
      
        // get_history:function(){
        //      // 添加下列代码, 发送请求, 获取用户的浏览记录信息:
        //     axios.get(this.host + '/browse_histories/', {
        //             responseType: 'json',
        //             withCredentials:true,
        //         })
        //         .then(response => {
        //             this.histories = response.data.skus;
        //             for(var i=0; i<this.histories.length; i++){
        //               this.histories[i].url='/goods/'+this.histories[i].id + '.html';
        //             }
        //         })
        //         .catch(error => {
        //             console.log(error)
        //         });
        // },
        // 获取用户所有的资料
        get_person_info: function () {
            let token = localStorage.token;
            var url = this.host + '/info/';
            axios.get(url, 
                {
                    headers: {
                      'Authorization': 'JWT ' + token
                    },
                    responseType: 'json',
                  })
                .then(response => {
                    if (response.data.code == 400) {
                        location.href = 'login.html'
                        return
                    }
                    this.username = response.data.info_data.username;
                    this.mobile = response.data.info_data.mobile;
                    this.email = response.data.info_data.email;
                    this.last_login = response.data.info_data.last_login;
                    this.email_active = response.data.info_data.email_active;
                })
                .catch(error => {
                    location.href = 'login.html'
                })
        },


        save_email: function () {
            var re = /^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
            if (re.test(this.email)) {
                this.email_error = false;
            } else {
                this.email_error = true;
                return;
            }

            // 进行前端页面请求:
            var url = this.host + '/emails/'
            axios.put(url,
                {
                    email: this.email
                },
                {
                    responseType: 'json',
                    withCredentials:true,
                })
                // 成功请求的回调
                .then(response => {
                    this.set_email = false;
                    this.send_email_btn_disabled = true;
                    this.send_email_tip = '已发送验证邮件'
                })
                // 失败请求的回调:
                .catch(error => {
                    alert('请求失败, 失败原因:', error);
                });
        }
        ,

        check_username: function () {
            var re = /^[a-zA-Z0-9_-]{5,20}$/;
            var re2 = /^[0-9]+$/;
            if (re.test(this.name) && !re2.test(this.name)) {
                this.error_name = false;
            } else {
                this.error_name_message = '请输入5-20个字符的用户名且不能为纯数字';
                this.error_name = true;
            }
        },

        check_mobile: function () {
            var re = /^1[345789]\d{9}$/;
            if (re.test(this.phone)) {
                this.error_mobile = false;
            } else {
                this.error_mobile_message = '您输入的手机号格式不正确';
                this.error_mobile = true;
            }
        },

        check_email: function () {
            var re = /[a-z0-9][\w\\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$/;
            if (re.test(this.e_mail)) {
                this.error_eamil = false;
            } else {
                this.error_email_message = '您输入的邮箱地址格式错误';
                this.error_eamil = true;
            }
    },
        on_submit: function () {
            this.check_username();
            this.check_mobile();
            this.check_email();
                let token = localStorage.token;
            // 点击注册按钮之后, 发送请求 (下面的代码是通过请求体传参的)
                axios.post(this.host + '/update_info/', {
                    username: this.name,
                    mobile: this.phone,
                    email: this.e_mail,
                },{
                    headers: {
                    'Authorization': 'JWT ' + token
                  },
                      responseType: 'json',
                      withCredentials:true,

                 })
                    .then(response => {
                        if (response.data.code==0) {
                            alert('修改成功')
                            location.href = 'login.html';
                        }
                        if (response.data.code == 400) {
                            alert('修改失败')
                            // alert(response.data.errmsg)
                        }
                    })
                    .catch(error => {
                        if (error.response.code == 400) {
                           
                        } else {
                            console.log(error);
                        }
                    })
            }
    }
});